import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';
import { DatePipe } from '@angular/common';

declare const echarts: any;
@Component({
  selector: 'app-jcwttj.component',
  templateUrl: './jcwttj.component.html',
  styleUrls: ['./jcwttj.component.less']
})

export class JcwttjComponent implements OnInit {
  queryParam: any = {};
  lockHeadScroll = { y: window.innerHeight - 350 + 'px'};
  table_bjwttj: any = {
    _data: [],
    tableLoading: false,
  }
  table_bjlxtj: any = {
    data: [],
    tableLoading: false,
  }
  table_bjxbtj: any = {
    data: [],
    tableLoading: false,
  }


  constructor(
    private http: _HttpClient,
    private datePipe: DatePipe
  ) { }

  ngOnInit(): void {
    this.initDate();
    this.getTjInfo();
  }

  initDate() {
    const _date = new Date();
    this.queryParam._begindate = _date;
    this.queryParam._enddate = _date;
  }

  /**
   * 删除私立字段
   * @param param 
   */
  removePrivateFields(param) {
    Object.keys(param).forEach(key => {
      if (!(key.indexOf('_'))) {
        delete param[key];
      }
    });
  }

  formatDate(param) {
    if (param._begindate) {
      param.begindate = this.datePipe.transform(param._begindate, 'yyyy-MM-dd');
    }
    if (param._enddate) {
      param.enddate = this.datePipe.transform(param._enddate, 'yyyy-MM-dd');
    }
  }

  createPie(id, data) {
    echarts.init(document.getElementById(id)).setOption({
      tooltip: {
        trigger: 'item',
        formatter: '{b} : {c} ({d}%)'
      },
      series: [
        {
          type: 'pie',
          radius: '85%',
          center: ['50%', '50%'],
          selectedMode: 'single',
          avoidLabelOverlap: false,
          label: {
            normal: {
              show: false,
              position: 'center'
            }
          },
          grid: {
            'left': 0,
            'right': 0,
            'top': 0,
            'bottom': 0
          },
          labelLine: {
            normal: {
              show: false
            }
          },
          data
        }
      ]
    }, true);
  }

  getTjInfo() {
    this.getWtdj();
    this.getLx();
    this.getXb();
  }

  getWtdj() {
    const param = {
      begindate: '',
      enddate: '',
    }
    Object.assign(param, this.queryParam);
    this.formatDate(param);
    this.removePrivateFields(param);
    this.table_bjwttj.data = [];
    this.query_wtdj(param).subscribe(res => {
      const data = res.data[0];
      this.table_bjwttj.data = data;

      this.createPie('chart_bjwttj', data);
    })
  }
  getLx() {
    const param = {
      begindate: '',
      enddate: '',
    }
    Object.assign(param, this.queryParam);
    this.formatDate(param);
    this.removePrivateFields(param);
    this.table_bjlxtj.data = [];
    this.query_lx(param).subscribe(res => {
      const data = res.data[0];
      this.table_bjlxtj.data = data;
    })
  }
  getXb() {
    const param = {
      begindate: '',
      enddate: '',
    }
    Object.assign(param, this.queryParam);
    this.formatDate(param);
    this.removePrivateFields(param);
    this.table_bjxbtj.data = [];
    this.query_xb(param).subscribe(res => {
      const data = res.data[0];
      this.table_bjxbtj.data = data;
    })
  }

  /**
   * 报警问题等级统计
   * @param param begindate
   * @param param enddate
   */
  query_wtdj(param) {
    const dataurl = environment.project + '/jzjcProblem/groupbyLvl';
    return this.http.get(dataurl, param);
  }
  /**
   * 报警类型统计
   * @param param begindate
   * @param param enddate
   */
  query_lx(param) {
    const dataurl = environment.project + '/jzjcProblem/groupbyType';
    return this.http.get(dataurl, param);
  }
  /**
   * 报警线别统计
   * @param param begindate
   * @param param enddate
   */
  query_xb(param) {
    const dataurl = environment.project + '/jzjcProblem/groupbyLine';
    return this.http.get(dataurl, param);
  }

}
